<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>添加客户</title>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/layui/layui_v2/css/layui.css" media="all"/>
</head>
<body>
<div style="width: 550px;height: 550px;padding: 10px 0px 0px 20px;text-align: center;">
    <form id="add_customer_form" class="layui-form layui-form-pane"
          action="" method="post"
          lay-filter="add_customer_form">
        <input type="hidden" id="id_input" name="id" value="${cus.id}"/>

        <div class="layui-form-item">
            <label class="layui-form-label">客户姓名：</label>
            <div class="layui-input-inline">
                <input id="name_input" type="text" name="name" lay-verify="required" value="${cus.name}"
                       placeholder="请输入客户姓名"
                       class="layui-input ">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">联系方式：</label>
            <div class="layui-input-inline">
                <input type="tel" name="tel" value="${cus.tel}" lay-verify="phone" placeholder="请输入联系方式"
                       autocomplete="off"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">客户地址：</label>
            <div class="layui-input-inline">
                <input id="address_input" type="text" lay-verify="required" name="address" value="${cus.address}"
                       placeholder="请输入客户地址"
                       class="layui-input ">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">所属员工</label>

            <div class="layui-input-inline">
                <select id="search_dept" lay-filter="search_dept" lay-search="">

                </select>
            </div>
            <div class="layui-input-inline">
                <select id="search_name" name="emp.id" lay-verify="required" lay-filter="search_name">

                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block" style="margin-top: 15px;">
                <button class="layui-btn layui-btn-big" style="margin-left: -150px;" lay-submit>修改客户</button>
            </div>
        </div>
    </form>

</div>

<script src="${pageContext.request.contextPath}/layui/layui_v2/layui.js"></script>
<script>
    layui.use(['element', 'table', 'jquery', 'layer', 'form'], function () {
        var $ = layui.jquery,
            table = layui.table,
            element = layui.element,
            layer = layui.layer,
            form = layui.form;

        var empid = ${cus.emp.id};
        var deptid = ${cus.emp.dept.id};

        renderSelectDepts();
        renderSelectEmpNames(deptid);

        //监听部门选择
        form.on('select(search_dept)', function (data) {
//            layer.msg(data.value);
            var detpid = data.value;
            //重新渲染姓名下拉框
            renderSelectEmpNames(detpid);
        });

        //渲染部门下拉框
        function renderSelectDepts() {
            //部门动态添加部门选择
            $.post("${pageContext.request.contextPath}/deptAction_findAllDeptNames.action", function (data) {

                var options = '<option value="">--请选择--</option>';

                for (var i = 0; i < data.length; i++) {
                    //拼字符串时千万不能出错，尤其是value不能有空格
                    if (data[i].id === deptid) {
                        options += '<option selected="selected" value="' + data[i].id + '">' + data[i].name + '</option>';
                    } else {
                        options += '<option value="' + data[i].id + '">' + data[i].name + '</option>';
                    }
                }
                $("#search_dept").append(options);

                form.render();
            });
        }

        //渲染员工姓名下拉框
        function renderSelectEmpNames(deptid) {
            $("#search_name").empty();
            var url = '${pageContext.request.contextPath}/empAction_findAllNames.action?is=1';

            if (deptid != null) {
                url += '&dept.id=' + deptid;
            }
//            layer.msg("url====" + url);
            //姓名选择
            $.post(url, function (data) {
                var options = '<option value="">--请选择--</option>';

                for (var i = 0; i < data.length; i++) {
                    //拼字符串时千万不能出错，尤其是value不能有空格
                    if (data[i].id === empid) {
                        options += '<option selected="selected" value="' + data[i].id + '">' + data[i].truename + '</option>';
                    } else {
                        options += '<option value="' + data[i].id + '">' + data[i].truename + '</option>';
                    }

                }
                $("#search_name").append(options);

                form.render();
            });

        }

        var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
        //监听表单提交
        form.on('submit(add_customer_form)', function () {
            // layer.msg("TT");

            $.post("${pageContext.request.contextPath}/customerAction_update.action",
                $("#add_customer_form").serialize(),
                function (data) {
//                        parent.layer.alert(data);
                    <%--parent.location.href = '${pageContext.request.contextPath}/pages_customer_customer.action';--%>
                    parent.location.reload();
                    parent.layer.close(index);

                });

            return false;
        });


    });
</script>

</body>
</html>
